const query = require('../config/db.config.js')
const getDepName = require('../utils/getDepName.Sql')

const userMod = {

  isExistMod: async (payload) => {
    // console.log(payload);
  
    let sql = `SELECT * FROM userinfo WHERE userId="${payload.uid}"`
    const code = await query(sql)
    if (code.length != 0) {
      return 
    } else {
      // 用户不存在,则创建数据库记录
      let createUserSql = 
      `INSERT INTO userinfo 
        (userId,resumeId,nickName,gender,avatarUrl) 
      VALUES 
        ("${payload.uid}", "${payload.uid}", "${payload.nickName}",${payload.gender},"${payload.avatarUrl}" );`

      let createResmSql = 
      `INSERT INTO resumeinfo 
        (resumeId) 
      VALUES ( "${payload.uid}" );`
      // 用户id 与简历表id 一致
      await query(createUserSql)
      await query(createResmSql)
      return 
    }
  },
  getContactMod: async (uid) =>{
    let sql = 
    `SELECT  
      contact
    FROM 
      userinfo 
    WHERE 
      userId = "${uid}"`
    const res = await query(sql)
    return res
  },
  // 更新信息
  editResumeMod: async (params) => {
    // console.log(params.skills);
    // 更新基本信息
    let baseSql = 
    `UPDATE userinfo 
     set 
      grade = "${params.grade}", 
      nickName = "${params.nickName}" ,
      gender = ${params.gender}, 
      department="${params.department}",
      schoolId=${params.schoolId} 
    WHERE 
      userId="${params.userId}"`
      // 更新简历信息
    await query(baseSql)
    let occ = ''
    let sql = 
    `UPDATE resumeinfo 
     SET 
      skills = "${params.skills || occ}" ,
      selfdesc = "${params.selfdesc || occ}", 
      exp="${params.exp || occ}" 
    WHERE 
      resumeId = "${params.userId}"`
    await query(sql)
  },
  // 获取用户信息
  getUserinfoMod: async (uid) => {
    let sql = 
    `SELECT  
      us.avatarUrl,
      us.nickName,
      us.gender,
      us.grade,
      re.selfdesc,
      re.skills,
      re.exp,
      dp.depName,
      us.department,
      sch.schoolId,
      sch.schoolName
    FROM 
      schoolinfo sch,
      resumeinfo re,
      userinfo us,
      depinfo dp
    WHERE 
      dp.depId = us.department
      AND us.userId = re.resumeId
      AND us.schoolId = sch.schoolId
      AND us.userId = "${uid}"`
    
    const res = await query(sql)
    console.log(res);
    return res
  },
  // 反馈
  sendFeedbackMod: async (params)=>{
    let {userId, info } = params
    let sql = 
    `INSERT INTO fbinfo
      (userId, info)
    VALUES
      ( "${userId}", "${info}")`
    await query(sql)
  }
}

module.exports = userMod